package com.situ.company.dao;


import com.situ.company.entity.User;
import com.situ.company.util.DBUtil;
import com.situ.company.util.StringUtil;
import com.situ.company.vo.UserVO;


import java.util.List;

public class UserDao {
    public void insert(User u){
        String sql="insert into user (username,password,user.name,create_time,dept_id)values(?,?,?,?,?)";
        DBUtil.executeDML(sql,u.getUsername(),u.getPassword(),u.getName(),u.getCreateTime(),u.getDeptId());
    }

    public List<UserVO> list(Integer page, Integer limit, String username, String name){
        String sql ="select user.*,dept.name 'deptName' from user left join dept on dept.id=user.dept_id where status = 0";
        if(StringUtil.isNotBlank(name)&&StringUtil.isNotBlank(username)) {
            sql += " and instr(user.name,?)>0 and instr(username,?)>0";
            sql += " limit ?,?";
            return DBUtil.executeDQL(sql, UserVO.class, name,username,(page - 1) * limit, limit);
        }
        if(StringUtil.isNotBlank(username)){
            sql += " and instr(username,?)>0 limit ?,?";
            return DBUtil.executeDQL(sql, UserVO.class, username,(page - 1) * limit, limit);
        }
        if(StringUtil.isNotBlank(name)){
            sql += " and instr(user.name,?)>0 limit ?,?";
            return DBUtil.executeDQL(sql, UserVO.class, name,(page - 1) * limit, limit);
        }
        sql += " limit ?,?";
        return DBUtil.executeDQL(sql, UserVO.class, (page - 1) * limit, limit);
    }

    public void delete(Integer id) {
        String sql ="update user set status = -1 where id=?";
        DBUtil.executeDML(sql,id);
    }

    public List<User> edit(Integer id) {
        String sql="select * from user where id =? and status = 0";
        return DBUtil.executeDQL(sql,User.class,id);
    }

    public void update(User u) {
        String sql ="update user set username=?,user.name=?,dept_id=? where id =? and status = 0";
        DBUtil.executeDML(sql,u.getUsername(),u.getName(),u.getDeptId(),u.getId());
    }

    public List<User> selectByUsername(String username) {
        String sql ="select * from user where username=? and status = 0";
        return DBUtil.executeDQL(sql,User.class,username);
    }

    public Long count(String username, String name) {
        String sql ="select count(1) from user where status = 0";
        if(StringUtil.isNotBlank(name)&&StringUtil.isNotBlank(username)) {
            sql += " and instr(name,?)>0 and instr(username,?)>0";
            return DBUtil.executeCount(sql,name,username);
        }
        if(StringUtil.isNotBlank(username)){
            sql += " and instr(username,?)>0 ";
            return DBUtil.executeCount(sql,username);
        }
        if(StringUtil.isNotBlank(name)){
            sql += " and instr(name,?)>0 ";
            return DBUtil.executeCount(sql, name);
        }
        return DBUtil.executeCount(sql);
    }

    public List<User> selectById(Integer id) {
        String sql = "select * from user where id=? and status = 0";
        return DBUtil.executeDQL(sql,User.class,id);
    }

    public void resetPassword(String newpassword1,String s) {
        String sql = "update user set password = ? where username = ? and status = 0";
        DBUtil.executeDML(sql,StringUtil.md5(newpassword1),s);
    }
}
